//
//  KCHomePageVC.m
//  Golden
//
//  Created by king on 2017/8/16.
//  Copyright © 2017年 KC. All rights reserved.
//

#import "KCKit.h"
#import "KCHomePageVC.h"
#import "KCBannerModel.h"
#import "WTCycleScrollView.h"
#import "KCHomeModel.h"
#import "KCHomeHeadView.h"
#import "KCHomeFootView.h"
#import "KCHomeTitleCell.h"
#import "KCHomeUserCell.h"
#import "KCHomeAuthorCell.h"
#import "KCHomeFreeCell.h"

@interface KCHomePageVC ()<KCStationDelegate,UITableViewDelegate,UITableViewDataSource,WTCycleScrollViewDelegate,WTCycleScrollViewDataSource>{
    
    WTCycleScrollView *headScroll;
    UIView *headView;
    NSArray *titleArray;
    NSArray *detailTitleArray;
    NSArray *heightArray;
    NSArray *rowArray;
    
}

@property (nonatomic,strong)KCStation *station;
@property (nonatomic,strong)KCHomeModel *dataModel;
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)NSMutableArray <KCBannerModel *>*bannerArray;

@end

@implementation KCHomePageVC

#pragma mark - LifeCycle

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createHome];
    [self requestHome];
    [self requestBanner];
}

- (void)dealloc {
    [KCStation RevokeStation:_station];
    KCLog(@"♻️ Dealloc %@", NSStringFromClass([self class]));
}

#pragma mark - Setup

- (void)createHome{
    
    self.KCTitleLable.text = @"取金";
    _station = [KCStation RegisterStation:KHttpStationHome delegate:self];
    _dataArray = [NSMutableArray array];
    _bannerArray = [NSMutableArray array];
    titleArray = @[@"取金头条",@"热门专栏",@"推荐作者",@"限免观点"];
    detailTitleArray = @[@"免费",@"",@"",@"免费"];
    heightArray = @[@"30",@"120",@"120",@"80"];
    rowArray = @[@"4",@"3",@"1",@"10"];
    _tableView = [[UITableView alloc]initWithFrame:TABLE_FRAME_S style:TABLE_STYLE_G];
    _tableView.backgroundColor = COLOR_FOR_BG;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = 0;
    [self.view addSubview:_tableView];
    
    headView = [[UIView alloc]initWithFrame:CGRectMake(X_OFF_SET, Y_OFF_SET, SCREEN_WIDTH, 150)];
    headView.backgroundColor = COLOR_FOR_BG;
    headScroll = [[WTCycleScrollView alloc]initWithFrame:CGRectMake(X_OFF_SET, Y_OFF_SET, SCREEN_WIDTH, 150)];
    headScroll.delegate = self;
    headScroll.dataSource = self;
    headScroll.backgroundColor = COLOR_FOR_BG;
    headScroll.pageType = pageCenter;
    [headView addSubview:headScroll];
    
    _tableView.tableHeaderView = headView;
}


- (void)requestHome{
    
    NSDictionary *params = @{@"token":@"CUqu9RmgZJ4qDJ5jQL1l5W057rbQm2sn",
                             @"uid":@"11207",
                             @"start":@"0",
                             @"size":@"20"};
    NSDictionary *KCHttp_Obj = @{KC_URL:[NSString ServiceStr:KCServiceHome],KC_PARAMS_DATA:params};
    [_station sendCargo:KHttpStation_Post cargoID:0x02 cargoObj:KCHttp_Obj];
    
}
- (void)requestBanner{
    
    NSDictionary *params = @{@"token":@"CUqu9RmgZJ4qDJ5jQL1l5W057rbQm2sn",
                             @"uid":@"11207"};
    NSDictionary *KCHttp_Obj = @{KC_URL:[NSString ServiceStr:KCServiceHomeBanner],KC_PARAMS_DATA:params};
    [_station sendCargo:KHttpStation_Post cargoID:0x01 cargoObj:KCHttp_Obj];
    
}

#pragma mark - 网络请求回掉

- (id)dispatch:(KCCargo *)cargo{
    
    if (cargo.cargoID == 0x01) {
    
        NSDictionary *dic = (NSDictionary *)cargo.cargoObj;
        NSDictionary *atDic = dic[@"attachment"];
        NSArray *bannArray = atDic[@"banners"];
        
        [bannArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
           
            KCBannerModel *model = [KCBannerModel modelWithDictionary:obj];
            [_bannerArray addObject:model];
            KCLog(@"1 %@",model.image);
            
        }];
        
        [headScroll reloadData];

    }else if (cargo.cargoID == 0x02) {
        
        KCLogFunc;
        NSDictionary *dic = (NSDictionary *)cargo.cargoObj;
        _dataModel = [KCHomeModel modelWithDictionary:dic[@"attachment"]];
        if (_dataModel) {
            [_dataArray addObject:_dataModel.headlines];
            [_dataArray addObject:_dataModel.ploys];
            [_dataArray addObject:_dataModel.authors];
            [_dataArray addObject:_dataModel.frees];
            [_tableView reloadData];
        }
        
    }
    
    return nil;
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return _dataArray.count;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [rowArray[section]integerValue];
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
       KCHomeTitleCell *cell = [KCHomeTitleCell cellWithTableView:tableView];
        cell.model = _dataArray[indexPath.section][indexPath.row];
        return cell;
    }else if (indexPath.section == 1){
       KCHomeUserCell *cell = [KCHomeUserCell cellWithTableView:tableView];
        cell.model = _dataArray[indexPath.section][indexPath.row];
        return cell;
    }else if (indexPath.section == 2){
       KCHomeAuthorCell *cell = [KCHomeAuthorCell cellWithTableView:tableView];
        cell.model = _dataArray[indexPath.section][indexPath.row];
        cell.anoModel = _dataArray[indexPath.section][(indexPath.row + 1)];
        return cell;
    }else{
       KCHomeFreeCell *cell = [KCHomeFreeCell cellWithTableView:tableView];
        cell.model = _dataArray[indexPath.section][indexPath.row];
        return cell;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [heightArray[indexPath.section]integerValue];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 50;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return [KCHomeHeadView headView:titleArray[section] :detailTitleArray[section]];
    
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    return [KCHomeFootView footView:section];

}

#pragma mark - scrollview代理方法

-(NSInteger)numberOfViewInCycleScrollView:(WTCycleScrollView*)view{
    return _bannerArray.count;
}
-(UIView*)cycleScrollView:(WTCycleScrollView*)view viewForIndex:(NSInteger)index{
    UIImageView *image = [[UIImageView alloc]init];
    image.contentMode = UIViewContentModeScaleAspectFill;
    image.clipsToBounds = YES;
    return image;
}
-(void)wtCycleScrollView:(WTCycleScrollView*)view didPressWithIndex:(NSInteger)index{
    
}


@end
